﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="App_Code.Modules.ContentModule" %>
<%@ Import Namespace="Portal.Core.Binding" %>
<%@ Import Namespace="Portal.Data" %>
<%@ Import Namespace="Portal.Data.Entity" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<asp:PlaceHolder runat="server" ID="phContent">
    <div class="filter">
        <h3>Thêm chương</h3>
    </div>
    <table class="Form">
        <tr>
            <td style="width: 15%">Chọn book
            </td>
            <td>
                <select id="books" runat="server" style="width: 565px;">
                </select>
                <input type="text" id="bookSearch" placeholder="Gõ tên truyện + enter để tìm kiếm"
                    value="" style="width: 250px;" />
            </td>
        </tr>
        <tr>
            <td style="width: 15%">Chương <span class="red bold">(*)</span>
            </td>
            <td>
                <asp:TextBox ID="Chapter" runat="server" Width="100"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 15%">Tiêu đề <span class="red bold">(*)</span>
            </td>
            <td>
                <asp:TextBox ID="Title" runat="server" Width="90%"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Nội dung <span class="red bold">(*)</span>
            </td>
            <td>
                <telerik:RadEditor ID="Body" runat="server" ToolsFile="~/cms_client/Editor_Rad_Full.xml"
                    Height="500" Width="99%">
                </telerik:RadEditor>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <asp:CheckBox ID="IsLastChap" Text="Đây là chương cuối" runat="server" /><br />
                <em>Nếu click chọn thì sách sẽ chuyển sang trạng thái kết thúc!</em>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <hr />
            </td>
        </tr>
        <tr>
            <td>CrawlName
            </td>
            <td>
                <asp:TextBox ID="CrawlName" runat="server" Width="90%"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>CrawlUrl
            </td>
            <td>
                <asp:TextBox ID="CrawlUrl" runat="server" Width="65%"></asp:TextBox>
                <asp:Button ID="btnCrawlOnline" runat="server" Text="Lấy nội dung online" OnClick="OnCrawl" />
            </td>
        </tr>
    </table>
    <br class="clear" />
    <asp:Button ID="Save" CssClass="CommandButton" runat="server" Text="Save" OnClientClick="return checkValid();"
        OnClick="Save_Click" />
    <asp:Button ID="Cancel" runat="server" CssClass="CommandButton" OnClick="Cancel_Click"
        Text="Cancel" ValidationGroup="xxxx" />
</asp:PlaceHolder>
<script type="text/javascript">
    function checkValid() {
        var book = jQuery('#<%=books.ClientID %>');
        if (book.val() == null) {
            alert("Hãy chọn 1 quyển truyện!");
            book.focus();
            return false;
        }
        var chapter = jQuery('#<%=Chapter.ClientID %>');
        if (!jQuery.isNumeric(chapter.val())) {
            chapter.val('');
            alert("Hãy điền số chương!");
            chapter.focus();
            return false;
        }
        var title = jQuery('#<%=Title.ClientID %>');
        if (title.val().length == 0) {
            alert("Thiếu tiêu đề!");
            title.focus();
            return false;
        }
        return true;
    }

    jQuery(function ($) {
        var sl = $('#<%=books.ClientID %>');
        sl.change(function () {
            var bid = sl.val();
            $.ajax({
                url: '/Ajax.aspx',
                dataType: 'json',
                data: {
                    p: 'Book',
                    act: 'getAutoNextChap',
                    bid: bid
                },
                success: function (data) {
                    loading(false);
                    $('#<%=Chapter.ClientID %>').val(data.nextchapter);
                },
                error: function (data) { console.log(data); loading(false); }
            });
        });
        $('#bookSearch').keyup(function (e) {
            var key = e.keyCode || e.which;
            if (key === 13) {
                sl.empty();
                var keyword = $(this).val();
                if (keyword.length > 0) {
                    loading(true);
                    $.ajax({
                        url: '/Ajax.aspx',
                        dataType: 'json',
                        data: {
                            p: 'Book',
                            act: 'load',
                            keyword: keyword
                        },
                        success: function (data) {
                            loading(false);
                            if (data.label.length) {
                                for (var i in data.label) {
                                    sl.append('<option value="' + data.id[i] + '">' + data.label[i] + '</option>');
                                }
                                sl.trigger("change");
                            }
                        },
                        error: function (data) { console.log(data); loading(false); }
                    });
                }
            }
        });
    });
</script>
<script runat="server">
    protected override Control Root
    {
        get
        {
            return phContent;
        }
    }
    private Portal.Data.Entity.Content _content;
    protected Portal.Data.Entity.Content NewContent
    {
        get
        {
            if (_content == null)
            {
                object obj = Binder.GetData(typeof(Portal.Data.Entity.Content), phContent, new BindContext());
                if (obj != null)
                {
                    _content = (Portal.Data.Entity.Content)obj;
                }
                else
                {
                    _content = new Portal.Data.Entity.Content();
                }
            }
            return _content;
        }
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!IsPostBack)
        {
            int bid;
            int.TryParse(Request["bid"], out bid);
            var b = Book.FindByPrimaryKey(bid);
            if (b != null)
            {
                var display = string.Format(PortalContext.BookByAccountDisplayFormat, b.ID, b.FullName, b.TotalChap, b.TotalVisit, b.Status);
                books.Items.Add(new ListItem(display, b.ID.ToString()));
                var lastChap = Portal.Data.Entity.Content.FindLatest(bid);
                Chapter.Text = lastChap!=null? (lastChap.Chapter + 1).ToString() : "1";
            }

            SetRadEditorPlugin(Body);
        }
    }
    void SetRadEditorPlugin(RadEditor editor)
    {
        editor.DisableFilter(EditorFilters.ConvertCharactersToEntities | EditorFilters.RemoveScripts);
        editor.EditModes = EditModes.Design | EditModes.Html;
        editor.StripFormattingOptions = EditorStripFormattingOptions.MSWord |
                                                            EditorStripFormattingOptions.MSWordNoFonts |
                                                            EditorStripFormattingOptions.Css |
                                                            EditorStripFormattingOptions.Font |
                                                            EditorStripFormattingOptions.Span |
                                                            EditorStripFormattingOptions.ConvertWordLists;
    }
    protected override void BeforeSave(object obj)
    {
        var c = (Portal.Data.Entity.Content)obj;
        c.Book_ID = Convert.ToInt32(Request.Form[books.Name]);
        c.BodyTemp = Body.Content;
        base.BeforeSave(c);
    }
    protected override void AfterSave(object obj)
    {
        var c = (Portal.Data.Entity.Content)obj;
        var b = c.GetBook();    //get new data

        var bFlag = false;
        if (!b.Finish && IsLastChap.Checked)
        {
            bFlag = true;
            b.Finish = true;
        }
        if (!b.Status)
        {
            bFlag = true;
            b.Status = true;
        }

        if (bFlag)
            b.UpdateOnly();
      
        base.AfterSave(obj);
    }
    protected void OnCrawl(object sender, EventArgs e)
    {
        Body.Content = Portal.Data.Entity.Content.Crawler(CrawlUrl.Text);
    }
</script>
